package com.cn.yugan.Activity;


import android.graphics.Bitmap;
import android.graphics.Movie;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.cn.yugan.Widget.GifView.GifView;
import com.cn.yugan.mobile.R;
import com.cn.yugan.Utils.ImgUtils;
import com.cn.yugan.Utils.LogUtils;
import com.cn.yugan.Utils.ToastUtils;
import com.cn.yugan.Widget.ZoomableImageView.ZoomableImageView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.apache.http.Header;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class ShowWebImageActivity extends AppCompatActivity {
    private String imagePath = null;
    private ZoomableImageView imageView = null;
    private ImageView show_imageview_download;
    private GifView show_webimage_gifview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_web_image);
        this.imagePath = getIntent().getStringExtra("image");
        imageView = (ZoomableImageView) findViewById(R.id.show_webimage_imageview);
        show_imageview_download = (ImageView) findViewById(R.id.show_imageview_download);
        show_webimage_gifview = (GifView) findViewById(R.id.show_webimage_gifview);

        LogUtils.d(imagePath);

        AsyncHttpClient asyncHttpClient = new AsyncHttpClient(true,80,443);
        asyncHttpClient.get(imagePath, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {

                if (imagePath.contains(".gif")) {
                    Movie movie = Movie.decodeByteArray(bytes, 0, bytes.length);
                    if (movie != null) {
                        show_webimage_gifview.setMovie(movie);
                        show_imageview_download.setOnClickListener(new onClickListener(imagePath, null, bytes));
                    }
                } else {
                    Drawable d = ImgUtils.Bytes2Drawable(bytes);
                    if (d != null) {
                        imageView.setImageBitmap(((BitmapDrawable) d).getBitmap());
                        show_imageview_download.setOnClickListener(new onClickListener(imagePath, d, null));
                    }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                ToastUtils.show(ShowWebImageActivity.this, "网络错误");
            }
        });
    }

    private class onClickListener implements View.OnClickListener {
        private String mimagepath;
        private Drawable md;
        private byte[] bytes;

        public onClickListener(String imagepath, @Nullable Drawable d, @Nullable byte[] bytes) {
            this.mimagepath = imagepath;
            this.md = d;
            this.bytes = bytes;
        }

        @Override
        public void onClick(View v) {
            String uri = getString(R.string.imageuri);
            File file = new File(uri);
            if (!file.exists()) {
                file.mkdirs();
            }
            String imagename = mimagepath.substring(mimagepath.lastIndexOf("/") + 1);
            Bitmap b;
            LogUtils.d(imagename);
            File file1 = new File(uri + imagename);
            if (file1.exists()) {
                ToastUtils.show(ShowWebImageActivity.this, "文件已存在");
            } else {
                try {
                    FileOutputStream out = new FileOutputStream(file1);

                    switch (mimagepath.substring(mimagepath.lastIndexOf(".") + 1)) {
                        case "jpg":
                            b = ((BitmapDrawable) md).getBitmap();
                            b.compress(Bitmap.CompressFormat.JPEG, 100, out);
                            break;
                        case "png":
                            b = ((BitmapDrawable) md).getBitmap();
                            b.compress(Bitmap.CompressFormat.PNG, 100, out);
                            break;
                        case "webp":
                            b = ((BitmapDrawable) md).getBitmap();
                            b.compress(Bitmap.CompressFormat.WEBP, 100, out);
                            break;
                        case "gif":
                            out.write(bytes);
                        default:
                            break;
                    }
                    out.flush();
                    out.close();
                    ToastUtils.show(ShowWebImageActivity.this, "保存成功");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    ToastUtils.show(ShowWebImageActivity.this, "保存失败");
                } catch (IOException e) {
                    e.printStackTrace();
                    ToastUtils.show(ShowWebImageActivity.this, "保存失败");
                }
            }
        }
    }
}


